
<template>
  <div class="back_img">
    <div class="background">
      <div class="login">
        <div class="login_title">
          <h1 class="bigtitle">{{'登录' }}</h1>
        </div>
        <el-form
                 ref="loginForm"
                 :model="loginForm"
                 :rules="loginRules"
                 status-icon
        >
          <el-form-item prop="name">
            <el-input v-model="loginForm.username" auto-complete="off" placeholder="请输入用户名"
                      prefix-icon="el-icon-user" type="text"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" auto-complete="off" placeholder="请输入密码"
                      prefix-icon="el-icon-unlock" type="password" @keyup.enter.native="handleLogin"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" class="homeBut" size="mini"
                       type="primary" @click.native.prevent="handleLogin">
              登录
            </el-button>
            <el-button class="homeBut" size="mini" type="primary" @click="tocmsRegister">
              注册
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="item">
        <div class="text">
          <h1 style="font-size: 30px">欢迎回来</h1>
          <h3>无论才能、知识多么卓越，如果缺乏热情，则无异于纸上画饼充饥，无补于事</h3>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "cmsLogin",
  data() {
    return {
      codeUrl: "",
      loginView:1,
      loginForm: {
        username: "admin",
        password: "123456",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    // this.getCode();
    this.getCookie();
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    tocmsRegister(){
      this.$router.push('/cmsRegister')
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: "/cms/main/cmsIndex" });
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style scoped>

body {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

.background {
  height: 400px;
  width: 620px;
  margin: 130px auto 0;
  box-shadow: 0 5px 9px rgba(.80, 0, 0, .80), 0 0 10px rgba(0, 0, 0, .80)
}

.item {
  width: 300px;
  height: 400px;

  float: right;
  margin: 0 0 0 0;
  padding: 10px 25px;

}

.text {
  text-align: center;
  margin-top: 40%;
  color: #ececec;
  font-family: STXinwei;
}


.homeBut {

  width: 100px;
  margin-bottom: 5px;
  margin-top: 18px;
  margin-left: 6.5%;

}

.login {
  width: 300px;
  height: 400px;

  float: right;
  margin: 0 0 0 0;


  padding: 10px 25px;


}

.login_title {
  text-align: center;
  margin-top: 20px;
}


.back_img {
  background: url("../../assets/images/wallhaven-k9p8l6.webp");
  background-size: 100% 100%;
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}

.bigtitle {
  font-family: "STXingkai";
  font-size: 45px;
}

.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
